package cn.mxz.controller;

import cn.hutool.extra.mail.MailUtil;
import cn.mxz.pojo.ResultData;
import cn.mxz.pojo.UserImage;
import cn.mxz.pojo.Users;
import cn.mxz.service.TbUserService;
import cn.mxz.service.impl.TbUserServiceImpl;
import cn.mxz.utils.PhoneUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者；朱洪旭
 * 时间；2021/12/6 18:02
 */
@RestController
@RequestMapping("/verification")
@CrossOrigin
public class VerificationController {
    @Autowired
    private TbUserServiceImpl tbUserService;
    String code=null;
    @GetMapping
    public ResultData emailverification(String email){
        if(email==null||email==""){return  new ResultData(121,"请填写邮箱");}
        String s = randomStr(6);
        this.code=s;
        try {
            MailUtil.send(email,"你的验证码是",s,false);
            return new ResultData(0,"发送");
        }catch (Exception e){
            return new ResultData(160,"你的邮箱格式不正确");
        }
    }
    @GetMapping("check")
    public  ResultData checkcode(String code,String email){
        if(code==null||code==""){return new ResultData(112,"验证码不能为空");}
        if(this.code.equalsIgnoreCase(code)){
            if(tbUserService.checkemail(email)){
            return  new ResultData(0,"邮箱可用验证码成功");
            }else{ return  new ResultData(110,"该邮箱已经被注册");
            }
        }
            return  new ResultData(111,"验证码不正确");
    }
    @GetMapping("/phone")
    public ResultData phonerveron(String phone){
        if(phone==""||phone==null){return new ResultData(115,"电话不能为空");}
         String s = randomStr(4);
         this.code=s;
         PhoneUtils.phonecode(phone,s);
         return new ResultData(0,"发送电话短信");
    }
    @PostMapping
    public ResultData usersinsert(String username,String password,String phone,String email,String birthday,String validateCode2){
        if(validateCode2==null){return new ResultData(119,"验证码不能为空");}
        if(!validateCode2.equalsIgnoreCase(this.code)){return  new ResultData(120,"验证码不正确");}
        if(username==null||password==null||username==""||password==""){return  new ResultData(116,"用户名或者密码不能为空");}
        if(tbUserService.checkeuser(username,phone)){return new ResultData(117,"账户名或者电话已经被注册");}
        Users users=new Users();
        users.setUsername(username);
        users.setPassword(password);
        users.setUserMobile(phone);
        users.setUserEmail(email);
        if(birthday!=null&birthday!=""){
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date  = sf.parse(birthday);
                users.setUserBirth(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        ResultData insertuser = tbUserService.insertuser(users);
        return  insertuser;
    }
    private String randomStr(int n) {
        String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        String str2 = "";
        int len = str1.length() - 1;
        double r;
        for (int i = 0; i < n; i++) {
            r = (Math.random()) * len;
            str2 = str2 + str1.charAt((int) r);
        }
        return str2;
    }


}
